package com.penghaisoft.microwms.product.service.impl;

import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.dto.HandResp;
import com.penghaisoft.microwms.common.dto.ProScrapOutSnDto;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.page.Pager;
import com.penghaisoft.microwms.common.service.BaseService;
import com.penghaisoft.microwms.common.wms.model.ProOrderInSn;
import com.penghaisoft.microwms.common.wms.model.ProScrapOutSn;
import com.penghaisoft.microwms.common.wms.model.ProStorageSn;
import com.penghaisoft.microwms.product.dao.*;
import com.penghaisoft.microwms.product.service.IProScrapOutSnService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 业务逻辑实现类
 * </p>
 *
 * @author
 * @createDate
 **/
@Service("proScrapOutSnService")
public class ProScrapOutSnServiceImpl extends BaseService implements IProScrapOutSnService {
    @Resource
    private ProScrapOutSnMapper proScrapOutSnMapper;

    @Resource
    private ProStorageSnMapper proStorageSnMapper;

    @Resource
    private MsbarCodeMapper msbarCodeMapper;
    
    @Resource
    private ProOrderInSnMapper proOrderInSnMapper;
    
    @Resource
    private ProBarcodeMapper proBarcodeMapper;

    /**
     * 新增记录
     *
     * @param
     * @return
     */
    @Override
    public Resp create(ProScrapOutSn proScrapOutSn) {
        proScrapOutSnMapper.create(proScrapOutSn);
        return success();
    }

    /**
     * 删除记录（逻辑删除）
     *
     * @param
     * @return
     */
    @Override
    public Resp delete(ProScrapOutSn proScrapOutSn) {
        proScrapOutSnMapper.delete(proScrapOutSn);
        return success();
    }

    /**
     * 查询列表
     *
     * @param
     * @param
     * @return
     */
    @Override
    public Pager<ProScrapOutSn> findListByCondition(int page, int rows, ProScrapOutSn condition) {
        Pager<ProScrapOutSn> pager = new Pager<>();
        pager.setPage(page);
        pager.setRows(rows);
        condition.preQuery();
        long size = proScrapOutSnMapper.queryCount(condition);
        List<ProScrapOutSn> records = new ArrayList<ProScrapOutSn>();
        if (size > 0) {
            records = proScrapOutSnMapper.queryList(pager, condition);
        }
        pager.setRecords(records);
        pager.setTotalCount(size);
        return pager;
    }

    /**
     * 查询单条
     *
     * @param id
     * @return
     */
    @Override
    public ProScrapOutSn findById(String id) {
        return proScrapOutSnMapper.queryById(id);
    }

    /**
     * 修改记录
     *
     * @param
     * @return
     */
    @Override
    public Resp update(ProScrapOutSn proScrapOutSn) {
        proScrapOutSnMapper.updateBySelect(proScrapOutSn);
        return success();
    }

    /**
     * wzhg 报废出库扫码，修改库存表中条码的activeFlag为0，往报废出库明细表中添加数据
     * @param proScrapOutSnDto
     * @return
     */
    @Override
    public HandResp scan(ProScrapOutSnDto proScrapOutSnDto) {
        HandResp resp = new HandResp();
        Date now = new Date();
        // 根据条码去库存明细表查询条码详情
        ProStorageSn proStorageSn = new ProStorageSn();
        proStorageSn.setBarcode(proScrapOutSnDto.getBarcode());
        proStorageSn.setActiveFlag("1");
        long count = proStorageSnMapper.queryCount(proStorageSn);
        if (count>1){
            resp.setCode(Constant.FAIL);
            resp.setMsg("库存中条码重复，请联系运维人员");
            return resp;
        }else if (count == 0){
            resp.setCode(Constant.FAIL);
            resp.setMsg("该条码不在库存中");
            return resp;
        }
        List<ProStorageSn> storageSns = new ArrayList<>();
        storageSns = proStorageSnMapper.queryByAny(proStorageSn);
        ProStorageSn storageSn = new ProStorageSn();
        storageSn = storageSns.get(0);
        if("2".equals(storageSn.getBarcodeStatus())){
        	resp.setCode(Constant.FAIL);
        	resp.setMsg("该条码已出库！");
        	return resp;
        }else if(!"0".equals(storageSn.getBarcodeStatus())){
        	resp.setCode(Constant.FAIL);
        	resp.setMsg("该条码已冻结！");
        	return resp;
        }
        // 往报废出库明细表中添加数据
        ProScrapOutSn proScrapOutSn = new ProScrapOutSn();
        proScrapOutSn.setUserDefined2(proScrapOutSnDto.getUserDefined2());
        proScrapOutSn.setBarcode(storageSn.getBarcode());
        proScrapOutSn.setModel(storageSn.getModel());
        proScrapOutSn.setProductName(storageSn.getProductName());
        proScrapOutSn.setDensity(storageSn.getDensity());
        proScrapOutSn.setSpec(storageSn.getSpec());
        proScrapOutSn.setWeight(storageSn.getWeight());
        proScrapOutSn.setUnit(storageSn.getUnit());
        proScrapOutSn.setMachineNumber(storageSn.getMachineNumber());
        proScrapOutSn.setGrade(storageSn.getGrade());
        proScrapOutSn.setLevel(storageSn.getLevel());
        proScrapOutSn.setType(storageSn.getType());
        proScrapOutSn.setWaCode(storageSn.getWaCode());
        proScrapOutSn.setWlCode(storageSn.getWlCode());
        proScrapOutSn.setUserDefined1(proScrapOutSnDto.getUserDefined1());
        proScrapOutSn.setCreateBy(proScrapOutSnDto.getLoginName());
        proScrapOutSn.setGmtCreate(now);
        proScrapOutSn.setLastModifiedBy(proScrapOutSnDto.getLoginName());
        proScrapOutSn.setGmtModified(now);
        proScrapOutSn.setActiveFlag("1");
//        报废状态
        proScrapOutSn.setUserDefined4("0");
        proScrapOutSnMapper.create(proScrapOutSn);
        
        //入库明细表中条码标记为报废
        ProOrderInSn proOrderInSn = new ProOrderInSn();
        proOrderInSn.setBarcode(proScrapOutSnDto.getBarcode());
        proOrderInSn.setUserDefined4("BF");
        proOrderInSnMapper.updateByBarcode(proOrderInSn);
        
        // 库存表中将该条码设为无效数据
        proStorageSn.setActiveFlag("0");
        proStorageSnMapper.updateByBarcode(proStorageSn);
        
        resp.setCode(Constant.SUCCESS);
        resp.setMsg("报废成功！");
        resp.setList(storageSns);
        return resp;
    }

	@Override
	public Long queryCount(ProScrapOutSn proScrapOutSn) {
		proScrapOutSn.preQuery();
		return proScrapOutSnMapper.queryCount(proScrapOutSn);
	}

	@Override
	public List<ProScrapOutSn> queryByAny(ProScrapOutSn proScrapOutSn) {
		List<ProScrapOutSn> list = proScrapOutSnMapper.queryByAny(proScrapOutSn);
		return list;
	}

	@Override
	public Pager<ProScrapOutSn> queryOrder(int page, int rows, ProScrapOutSn proscrapoutsn) {
		Pager<ProScrapOutSn> pager = new Pager<>();
		pager.setPage(page);
		pager.setRows(rows);
//        proscrapoutsn.preQuery();
        long size = proScrapOutSnMapper.queryCountPOutOrder(proscrapoutsn);
		List<ProScrapOutSn>  list = proScrapOutSnMapper.queryPOutOrder(pager,proscrapoutsn);
		if(list.size() > 0){
			ProScrapOutSn total = proScrapOutSnMapper.queryTotal(proscrapoutsn);
        	pager.setOrder("{\"codeNum\":\"" + total.getCodeNum()
        			+ "\",\"codeWt\":\"" + total.getCodeWt().stripTrailingZeros().toPlainString() + "\"}");
//            pager.setOrder("");
		}
		pager.setRecords(list);
		pager.setTotalCount(size);
		return pager;
	}

}
